import java.util.Arrays;

public class Solution289 {
    public void gameOfLife(int[][] board) {
        int[] dx=new int[]{-1,0,1,-1,1,-1,0,1};
        int[] dy=new int[]{-1,-1,-1,0,0,1,1,1};
        int m=board.length,n=board[0].length;
        for (int i=0;i<m;i++){
            for (int j=0;j<n;j++){
                int total=0;
                for (int k=0;k<8;k++){
                    int x=i+dx[k];
                    int y=j+dy[k];
                    if (x>=0 && x<m && y>=0 && y<n){
                        if (board[x][y]%2==1){
                            total++;
                        }
                    }
                }
                if (board[i][j]==0) {
                    if (total==3) {
                        board[i][j]=2;
                    }
                }else{
                    if (total<2 || total>3){
                        board[i][j]=3;
                    }
                }
            }
        }
        for (int i=0;i<m;i++){
            for (int j=0;j<n;j++){
                if (board[i][j]==3){
                    board[i][j]=0;
                }else if (board[i][j]==2){
                    board[i][j]=1;
                }
            }
        }
    }

    public static void main(String[] args) {
        int[][] board=new int[][]{{0,1,0},{0,0,1},{1,1,1},{0,0,0}};
        new Solution289().gameOfLife(board);
        System.out.println(Arrays.deepToString(board));
    }
}
